package com.xywl.escort.view.market;

import com.xywl.escort.bean.BannerReq;
import com.xywl.escort.bean.BannerResp;
import com.xywl.escort.bean.GoodListReq;
import com.xywl.escort.bean.MarketListResp;
import com.xywl.escort.net.BaseSubscriber;
import com.xywl.escort.net.ExceptionHandle;
import com.xywl.escort.net.api.service.ApiServer;
import com.xywl.escort.utils.RxUtil;

import javax.inject.Inject;

import timber.log.Timber;

/**
 * Created by 32422 on 2018/5/2.
 */

public class MarketPresenter implements MarketContract.Presenter {
    private MarketContract.View mView;
    private ApiServer mApiServer;

    @Inject
    MarketPresenter(ApiServer apiServer) {
        mApiServer = apiServer;
    }

    @Override
    public void subscribe() {

    }

    @Override
    public void takeView(MarketContract.View view) {
        this.mView = view;
    }

    @Override
    public void loadBanner() {
        BannerReq req = new BannerReq();
        req.position = 3;
        mApiServer.focusPicture(req)
                .compose(RxUtil.<BannerResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<BannerResp>bindToLife())
                .subscribe(new BaseSubscriber<BannerResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.loadBannerFailure();
                    }

                    @Override
                    public void onNext(BannerResp resp) {
                        mView.loadBannerSuccess(resp);
                    }
                });
    }

    @Override
    public void loadMarketList(GoodListReq req) {
        mApiServer.goodList(req)
                .compose(RxUtil.<MarketListResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<MarketListResp>bindToLife())
                .subscribe(new BaseSubscriber<MarketListResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.loadMarketFailure();
                    }

                    @Override
                    public void onNext(MarketListResp resp) {
                        if (null != resp && null != resp.data && resp.isOk()) {
                            MarketListResp.DataBean dataBean = resp.data;
                            mView.updateMarketList(dataBean);
                        } else {
                            mView.loadMarketFailure();
                        }
                    }
                });
    }

    @Override
    public void unSubscribe() {
        Timber.tag("MarketPresenter").e("unSubscribe");
        mView = null;
    }
}
